package com.breaktian.assemble.http.retrofit;



import com.breaktian.assemble.utils.LogUtil;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

//import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by breaktian on 2016/10/27.
 */

public class RetrofitProvider {

    private static RetrofitProvider instance;
    private Retrofit retrofit;
    private OkHttpClient mOkHttpClient;
    private static long HTTPTIMEOUT = 30*1000;
    private static String BASEURL = "http://www.hbounty.com/";


    private RetrofitProvider(){
        createRetrofitProvider();
    }

    private void createRetrofitProvider(){
        LogUtil.d("createRetrofitProvider");
        retrofit = new Retrofit.Builder()
                .baseUrl(BASEURL)
                //增加返回值为String的支持
                .addConverterFactory(ScalarsConverterFactory.create())
                //增加返回值为Gson的支持(以实体类返回)
                .addConverterFactory(GsonConverterFactory.create())
                //增加返回值为Oservable<T>的支持
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(createOkHttpClient())
                .build();

    }

    public static RetrofitProvider get(){
        if(instance==null){
            synchronized (RetrofitProvider.class){
                if(instance==null){
                    instance = new RetrofitProvider();
                }
            }
        }
        return instance;
    }


    private OkHttpClient createOkHttpClient(){
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        //包含header、body数据
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return new OkHttpClient.Builder()
                // .addNetworkInterceptor()
                .addInterceptor(loggingInterceptor)
                // 添加公共参数
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request();
//                        LogUtil.d("HTTP URL：" + request.url().toString());
//                        HttpUrl url = request.url().newBuilder()
//                                .addQueryParameter("*reqid*", RandomUtils.createRandomString(16))
//                                .build();
//                        request = request.newBuilder().url(url).build();
                        Response response = chain.proceed(request);
//                        String json = response.body().string();
//                        LogUtil.d("request:"+request.url());
//                        LogUtil.d("response:"+json);




                        return response;
                    }
                })
                .connectTimeout(HTTPTIMEOUT, TimeUnit.MILLISECONDS)       //设置连接超时
                .readTimeout(HTTPTIMEOUT,TimeUnit.MILLISECONDS)          //设置读取超时
                .writeTimeout(HTTPTIMEOUT,TimeUnit.MILLISECONDS)         //设置写入超时
//                .cache(new Cache(getCacheDir(),10 * 1024 * 1024))   //设置缓存目录和10M缓存
//                .addInterceptor(interceptor)    //添加日志拦截器（该方法也可以设置公共参数，头信息）
                .build();
    }


    public OkHttpClient getClient(){
        if(mOkHttpClient==null){
            mOkHttpClient = createOkHttpClient();
        }
        return mOkHttpClient;
    }




    public  <T> T create(Class<T> service){
        return retrofit.create(service);
    }


}
